﻿/// <reference path="../../yui-3.14-min.js" />
YUI.add('pregunta-list-view', function (Y) {
    Y.PreguntasView = Y.Base.create('PreguntasView', Y.View, [], {
        templateIsCharged: false,
        loadTemplate: function () {
            if (this.template == '') {
                var that = this,
                validator = null,
                request = Y.io('/ExcellentProject/scripts/views/cuestionarios/preguntas/templates/preguntas.html', {
                    on: {
                        complete: function (id, response) {
                            that.templateIsCharged = true;
                            that.template = response.responseText;
                            try {
                                $('#preguntas').parent().replaceWith('');
                            }
                            catch (err) {
                                console.log(err);
                            }
                            that.render();
                        }
                    },
                    failure: function (id, response) {
                        alert(response, 'Error al cargar Preguntas')
                    }
                })
            }
        },

        render: function () {
            this.loadTemplate();
            if (this.templateIsCharged) {
                this.get('container').setHTML(this.template);
                var list = this.get('modelList');
                var table = $('#preguntas').DataTable({
                    "language": {
                        "search": "Buscar:",
                        "lengthMenu": "Items por página: _MENU_",
                        "info": "Items del _START_ al _END_ de _TOTAL_ items.",
                        "emptyTable": "Items no disponibles.",
                        "infoEmpty": "0 ítems.",
                        "paginate": {
                            "next": "Siguiente",
                            "previous": "Anterior",
                            "first": "Primero",
                            "last": "Ultimo"
                        }
                    },
                    "paging": true,
                    retrieve: true,
                    data: list._items,
                    "rowCallback": function (row, aData) {
                        $('td:eq(5)', row).html('<a title="Editar" class="btn btn-warning" href="/preguntas/' + aData.get('IdPregunta') + '">' +
                          '<i class="fa fa-pencil">' + '</i>' + '<span class="tooltip"></span></a>');
                        $('td:eq(6)', row).html('<a title="Eliminar" class="btn btn-danger" href="/preguntas/eliminar/' + aData.get('IdPregunta') + '" >' +
                         '<i class="fa fa-trash-o">' + '</i>' + '<span class="tooltip"></span></a>');
                    }
                    ,
                    columns: [
                        { data: '_state.data.CodigoTipo.lazy.value' },
                        { data: '_state.data.Enunciado.lazy.value' },
                        { data: '_state.data.Categoria.lazy.value', width: "5em" },
                        { data: '_state.data.CodigoArea.lazy.value', width: "5em" },
                        { data: '_state.data.Estado.lazy.value', width: "5em" },
                        { data: '_state.data.IdPregunta.lazy.value' },
                        { data: '_state.data.IdPregunta.lazy.value' }
                    ]
                });
                table.draw();
            }
        },

    })

}, '0.0.0.1', {
    requires: ['view', 'pregunta-model-list', 'pregunta-model', 'editar-pregunta-view','io', 'handlebars']
})